import alibabacloud_oss_v2 as oss
import os
from fastapi import HTTPException
# 硬编码参数值
region = 'cn-beijing'
bucket = 'whatteam1'

# 硬编码凭证信息
access_key_id = "LTAI5tC4VPs9n2ZoLy8sptqR"
access_key_secret = "Gr6QeyWXCuwhTVVUk25x8FTTMLunwv"


def main(key:str,file_path:str):
    # 使用静态凭证信息
    credentials_provider = oss.credentials.StaticCredentialsProvider(
        access_key_id=access_key_id,
        access_key_secret=access_key_secret
    )

    # 加载SDK的默认配置，并设置凭证提供者
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    # 设置配置中的区域信息
    cfg.region = region

    # 使用配置好的信息创建OSS客户端
    client = oss.Client(cfg)

    # 读取 a.png 文件内容
    # file_path = '../text/c.png'  # 假设 a.png 在脚本同一目录下
    with open(file_path, 'rb') as file:
        data = file.read()  # 读取文件内容为二进制数据

    # 执行上传对象的请求，指定存储空间名称、对象名称和数据内容
    result = client.put_object(oss.PutObjectRequest(
        bucket=bucket,
        key=key,
        body=data,
        acl='public-read-write',
        headers={
            'Content-Disposition': 'inline'  # 关键修改：添加 Content-Disposition 头
        }
    ))

    # 构造文件访问URL
    endpoint = f"http://{bucket}.oss-{region}.aliyuncs.com"
    file_url = f"{endpoint}/{key}"

    # 输出请求结果和文件URL
    print(f'Upload successful! File URL: {file_url}')
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' content md5: {result.content_md5},'
          f' etag: {result.etag},'
          f' hash crc64: {result.hash_crc64},'
          f' version id: {result.version_id}'
          )



def set_content_disposition_to_inline():

        # 使用静态凭证信息
        credentials_provider = oss.credentials.StaticCredentialsProvider(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )

        # 加载SDK的默认配置，并设置凭证提供者
        cfg = oss.config.load_default()
        cfg.credentials_provider = credentials_provider
        cfg.region = region

        # 使用配置好的信息创建OSS客户端
        client = oss.Client(cfg)

        # 分页遍历Bucket中的所有对象
        marker = ''
        while True:
            list_objects_request = oss.ListObjectsRequest(bucket=bucket, marker=marker)
            result = client.list_objects(list_objects_request)

            for obj in result.contents:
                key = obj.key

                # 获取对象当前的元数据
                head_object_request = oss.HeadObjectRequest(bucket=bucket, key=key)
                head_result = client.head_object(head_object_request)

                # 更新 Content-Disposition 并保留其他头信息
                headers = head_result.headers
                headers['Content-Disposition'] = 'inline'

                # 使用 CopyObject 替换对象的元数据
                copy_object_request = oss.CopyObjectRequest(
                    bucket=bucket,
                    key=key,
                    source_bucket=bucket,
                    source_key=key,
                    metadata_directive='REPLACE',
                    headers=headers
                )
                copy_result = client.copy_object(copy_object_request)

                print(f'Updated Content-Disposition for {key} to inline. ETag: {copy_result.etag}')

            # 判断是否还有下一页
            if not result.is_truncated:
                break
            marker = result.next_marker

        return {"message": "Content-Disposition updated successfully for all objects"}



if __name__ == "__main__":
    file_path = '../text/d.png'
    main('s/b.png',file_path)